/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dbdir.core;

import java.util.ArrayList;
import org.dbdir.connections.TCPSession;
import org.dbdir.core.helper.ACL;
import org.dbdir.core.helper.Database;

/**
 *
 * @author thotheolh
 */
public class ProcessTCPCommand {

    private boolean isR = false;
    private boolean okRs = false; // for ArrayList answers
    private String strRs = ""; // for String answers
    private int intRs; // for int answers. -1 for not int answers.
    private ArrayList aRs = new ArrayList(); // for arraylist answers
    private AccessData data;
    private TCPSession session;
    //values
    private String val_Usr;
    private String val_Pwd;
    private String val_OldPwd;
    private String val_NewPwd;

    //keys
    public ProcessTCPCommand(AccessData data, TCPSession session) {
        this.data = data;
        this.session = session;
    }

    /**
     * Process TCP Commands
     */
    public void processCmd(String cmd) {
        String args[] = cmd.split("\\s+");
        if (args[0].equals("rmme")) {
            if (data.doDeleteUser(session.getUsername()) == true) {
                session.netout("rmme-ok");
            } else {
                session.netout("rmme-fail");
            }
        }

        if (args[0].equals("addusr")) {
            //session.output("Received 'addusr'\n");
            String[] value1 = args[1].split("=");
            String[] value2 = args[2].split("=");
            String username = null;
            String password = null;
            if (value1[0].equals("usr")) {
                username = value1[1];
            } else if (value1[0].equals("pwd")) {
                password = value1[1];
            }
            if (value2[0].equals("usr")) {
                username = value2[1];
            } else if (value2[0].equals("pwd")) {
                password = value2[1];
            }
            if (username == null || password == null) {
                //session.output("addusr - bad cmd\n");
            } else {
                if (data.isUserExist(username) != true) {
                    if (data.doRegisterUser(username, password) == true) {
                        session.netout("addusr-ok");
                    } else {
                        session.netout("addusr-fail");
                    }

                } else {
                    session.netout("addusr-exist");
                }
            }
        }

        if (args[0].equals("modme")) {
            //session.output("Received 'modme'\n");
            String[] value1 = args[1].split("=");
            String[] value2 = args[2].split("=");
            if (value1[0].equals("oldpwd")) {
                this.val_OldPwd = value1[1];
            } else if (value1[0].equals("newpwd")) {
                this.val_NewPwd = value1[1];
            }
            if (value2[0].equals("newpwd")) {
                this.val_NewPwd = value2[1];
            } else if (value2[0].equals("oldpwd")) {
                this.val_OldPwd = value2[1];
            }
            if (this.val_NewPwd == null || this.val_OldPwd == null) {
                //drops request if any error
                //session.output("modme - bad cmd\n");
            } else {
                if (data.doUpdateUser(session.getUsername(), this.val_OldPwd, this.val_NewPwd) == true) {
                    //output("User: " + this.val_Username + ", have SUCCESSFULLY been REGISTERED.\n\r");
                    session.netout("modme-ok");
                //System.out.println("modme - ok");
                } else {
                    //output("User: " + this.val_Username + ", have FAILED to be REGISTERED.\n\r");
                    session.netout("modme-fail");
                //System.out.println("modme - fail");
                }
            }
        }

        if (args[0].equals("rmusr")) {
            //session.output("Received 'rmusr'\n");
            String[] value1 = args[1].split("=");
            String[] value2 = args[2].split("=");
            String dbalias = null;
            String user = null;
            if (value1[0].equals("dbalias")) {
                dbalias = value1[1];
            } else if (value1[0].equals("usr")) {
                user = value1[1];
            }
            if (value2[0].equals("dbalias")) {
                dbalias = value2[1];
            } else if (value2[0].equals("usr")) {
                user = value2[1];
            }
            if (dbalias == null || user == null) {
                //drops request if any error
                session.output("rmusr - bad cmd\n");
            } else {
                if (data.isUserExistDB(user, dbalias)) {
                    if (data.rmUserFromDatabase(user, dbalias)) {
                        session.netout("rmusr-ok");
                    } else {
                        session.netout("rmusr-fail");
                    }
                }
            }
        }

        if (args[0].equals("getdb")) {
            //session.output("Received 'getdb'\n");
            String[] value1 = args[1].split("=");
            String value = value1[1];
            if (args[1].isEmpty() || (!value1[0].equals("dbalias"))) {
                //session.output("getdb - bad cmd\n");
            } else {
                if (data.isUserExistDB(session.getUsername(), value) == true) {
                    Database db = data.getDatabaseInfo(session.getUsername(), value);
                    session.netout("getdbrs " + db.getString());
                } else {
                    session.netout("getdb-not-exist");
                }
            }
        }


        if (args[0].equals("getusrls")) {
            String[] value1 = args[1].split("=");
            String value = value1[1];
            if (args[1].isEmpty() || (!value1[0].equals("dbalias"))) {
                //session.output("getdb - bad cmd\n");
            } else {
                if (data.isUserExistDB(session.getUsername(), value) == true) {
                    ArrayList usrList = data.getDBUserList(value,session.getUsername());
                    session.netout("getusrlsrs " + procArrayToString(usrList));
                } else {
                    session.netout("user-not-exist");
                }
            }
        }

        if (args[0].equals("getdbls")) {
            ArrayList dbList = data.getUserDBList(session.getUsername());
            session.netout("getdblsrs " + procArrayToString(dbList));
        }

        if (args[0].equalsIgnoreCase("mkdb")) {
            // session.output("Not Yet Implemented\n\r");
            String[] value1 = args[1].split("=");
            String[] value2 = args[2].split("=");
            String[] value3 = args[3].split("=");
            String[] value4 = args[4].split("=");
            String key_Alias = "dbalias";
            String key_DBUser = "dbusr";
            String key_DBPass = "dbpwd";
            String key_DBPath = "dbpath";
            String val_Alias = null;
            String val_DBUser = null;
            String val_DBPass = null;
            String val_DBPath = null;
            if (value1[0].equals(key_Alias)) {
                val_Alias = value1[1];
            } else if (value1[0].equals(key_DBUser)) {
                val_DBUser = value1[1];
            } else if (value1[0].equals(key_DBPass)) {
                val_DBPass = value1[1];
            } else if (value1[0].equals(key_DBPath)) {
                val_DBPath = value1[1];
            }
            if (value2[0].equals(key_Alias)) {
                val_Alias = value2[1];
            } else if (value2[0].equals(key_DBUser)) {
                val_DBUser = value2[1];
            } else if (value2[0].equals(key_DBPass)) {
                val_DBPass = value2[1];
            } else if (value2[0].equals(key_DBPath)) {
                val_DBPath = value2[1];
            }
            if (value3[0].equals(key_Alias)) {
                val_Alias = value3[1];
            } else if (value3[0].equals(key_DBUser)) {
                val_DBUser = value3[1];
            } else if (value3[0].equals(key_DBPass)) {
                val_DBPass = value3[1];
            } else if (value3[0].equals(key_DBPath)) {
                val_DBPath = value3[1];
            }
            if (value4[0].equals(key_Alias)) {
                val_Alias = value4[1];
            } else if (value4[0].equals(key_DBUser)) {
                val_DBUser = value4[1];
            } else if (value4[0].equals(key_DBPass)) {
                val_DBPass = value4[1];
            } else if (value4[0].equals(key_DBPath)) {
                val_DBPath = value4[1];
            }
            if (val_DBUser == null || val_DBPass == null || val_Alias == null || val_DBPath == null) {
                //session.output("mkdb - bad cmd\n");
            } else {
                Database db = new Database();
                db.setDbUsername(val_DBUser);
                db.setDbPassword(val_DBPass);
                db.setDbAlias(val_Alias);
                db.setDbPath(val_DBPath);
                if (data.registerDatabase(session.getUsername(), val_Alias, val_DBPath, val_DBUser, val_DBPass)) {
                    session.netout("mkdb-ok");
                } else {
                    session.netout("mkdb-fail");
                }
            }
        }

        if (args[0].equalsIgnoreCase("moddb")) {
            // session.output("Not Yet Implemented\n\r");
            String[] value1 = args[1].split("=");
            String[] value2 = args[2].split("=");
            String[] value3 = args[3].split("=");
            String[] value4 = args[4].split("=");
            String key_Alias = "dbalias";
            String key_DBUser = "dbusr";
            String key_DBPass = "dbpwd";
            String key_DBPath = "dbpath";
            String val_Alias = null;
            String val_DBUser = null;
            String val_DBPass = null;
            String val_DBPath = null;
            if (value1[0].equals(key_Alias)) {
                val_Alias = value1[1];
            } else if (value1[0].equals(key_DBUser)) {
                val_DBUser = value1[1];
            } else if (value1[0].equals(key_DBPass)) {
                val_DBPass = value1[1];
            } else if (value1[0].equals(key_DBPath)) {
                val_DBPath = value1[1];
            }
            if (value2[0].equals(key_Alias)) {
                val_Alias = value2[1];
            } else if (value2[0].equals(key_DBUser)) {
                val_DBUser = value2[1];
            } else if (value2[0].equals(key_DBPass)) {
                val_DBPass = value2[1];
            } else if (value2[0].equals(key_DBPath)) {
                val_DBPath = value2[1];
            }
            if (value3[0].equals(key_Alias)) {
                val_Alias = value3[1];
            } else if (value3[0].equals(key_DBUser)) {
                val_DBUser = value3[1];
            } else if (value3[0].equals(key_DBPass)) {
                val_DBPass = value3[1];
            } else if (value3[0].equals(key_DBPath)) {
                val_DBPath = value3[1];
            }
            if (value4[0].equals(key_Alias)) {
                val_Alias = value4[1];
            } else if (value4[0].equals(key_DBUser)) {
                val_DBUser = value4[1];
            } else if (value4[0].equals(key_DBPass)) {
                val_DBPass = value4[1];
            } else if (value4[0].equals(key_DBPath)) {
                val_DBPath = value4[1];
            }
            if (val_DBUser == null || val_DBPass == null || val_Alias == null || val_DBPath == null) {
                //session.output("mkdb - bad cmd\n");
            } else {
                Database db = new Database();
                db.setDbUsername(val_DBUser);
                db.setDbPassword(val_DBPass);
                db.setDbAlias(val_Alias);
                db.setDbPath(val_DBPath);
                if (data.updateDatabaseInfo(session.getUsername(), db)) {
                    session.netout("moddb-ok");
                } else {
                    session.netout("moddb-fail");
                }
            }
        }

        if (args[0].equalsIgnoreCase("deldb")) {
            // output("Not Yet Implemented\n\r");
            String[] value1 = args[1].split("=");
            String val_Alias = null;
            if (value1[0].equals("dbalias")) {
                val_Alias = value1[1];
            }
            if (val_Alias == null) {
                session.netout("deldb - bad cmd");
            } // output(value1[0]+","+value1[1]+","+value2[0]+","+value2[1]+"\n\r"); // diagnostic
            else {
                if (data.deleteDatabase(session.getUsername(), val_Alias)) {
                    session.netout("deldb-ok");
                } else {
                    session.netout("deldb-fail");
                }
            }
        }

        if (args[0].equals("joindb")) {
            //session.output("Received 'joindb'\n");
            String[] value1 = args[1].split("=");
            String value = value1[1];
            if (args[1].isEmpty() || (!value1[0].equals("dbalias"))) {
                //session.output("joindb - bad cmd\n");
            } else {
                if (data.isUserExistDB(session.getUsername(), value) == false) {
                    if (data.joinDatabase(session.getUsername(), value) == true) {
                        session.netout("joindb-ok");
                    } else {
                        session.netout("joindb-fail");
                    }

                } else {
                    session.netout("joindb-exist");
                }
            }
        }

        if (args[0].equals("quitdb")) {
            //session.output("Received 'quitdb'\n");
            String[] value1 = args[1].split("=");
            String value = value1[1];
            if (args[1].isEmpty() || (!value1[0].equals("dbalias"))) {
                //session.output("quitdb - bad cm\n");
            } else {
                if (data.isUserExistDB(session.getUsername(), value) == true) {
                    if (data.rmUserFromDatabase(session.getUsername(), value) == true) {
                        session.netout("quitdb-ok");
                    } else {
                        session.netout("quitdb-fail");
                    }

                } else {
                    session.netout("quitdb-not-exist");
                }
            }
        }

        if (args[0].equalsIgnoreCase("getacl")) {
            // output("Not Yet Implemented\n\r");
            String[] value1 = args[1].split("=");
            String[] value2 = args[2].split("=");
            String key_Username = "usr";
            String key_Alias = "dbalias";
            String val_Username = null;
            String val_Alias = null;
            if (value1[0].equals(key_Username)) {
                val_Username = value1[1];
            } else if (value1[0].equals(key_Alias)) {
                val_Alias = value1[1];
            }
            if (value2[0].equals(key_Username)) {
                val_Username = value2[1];
            } else if (value2[0].equals(key_Alias)) {
                val_Alias = value2[1];
            }
            if (val_Alias == null || val_Username == null) {
                //session.output("getacl - bad cmd\n");
            } else {
                ACL acl = data.getPermission(val_Username, val_Alias);
                if (acl != null) {
                    session.netout("getaclrs usr=" + acl.getUsername() + " dbalias=" + acl.getDbAlias() + " sdbpath=" + convertBoolToStr(acl.isShowDbPath()) + " sdbusr=" +
                            convertBoolToStr(acl.isShowDbUsername()) + " sdbpwd=" + convertBoolToStr(acl.isShowDbPassword()) +
                            " sdtreg=" + convertBoolToStr(acl.isShowDateTimeReg()) + " edbpath=" + convertBoolToStr(acl.isEditDbPath()) + " edbusr=" +
                            convertBoolToStr(acl.isEditDbUsername()) + " edbpwd=" + convertBoolToStr(acl.isEditDbPassword()));
                } else {
                    session.netout("getacl-not-exist");
                }
            }
        }

        if (args[0].equalsIgnoreCase("modacl")) {
            // output("Not Yet Implemented\n\r");
            String[] value1 = args[1].split("=");
            String[] value2 = args[2].split("=");
            String[] value3 = args[3].split("=");
            String[] value4 = args[4].split("=");
            String[] value5 = args[5].split("=");
            String[] value6 = args[6].split("=");
            String[] value7 = args[7].split("=");
            String[] value8 = args[8].split("=");
            String[] value9 = args[9].split("=");
            String key_Username = "usr";
            String key_Alias = "dbalias";
            String key_showDbPath = "sdbpath";
            String key_showDbUsername = "sdbusr";
            String key_showDbPassword = "sdbpwd";
            String key_showDateTimeReg = "sdtreg";
            String key_editDbPath = "edbpath";
            String key_editDbUsername = "edbusr";
            String key_editDbPassword = "edbpwd";
            String val_Username = null;
            String val_Alias = null;
            boolean val_showDbPath = false;
            boolean val_showDbUsername = false;
            boolean val_showDbPassword = false;
            boolean val_showDateTimeReg = false;
            boolean val_editDbPath = false;
            boolean val_editDbUsername = false;
            boolean val_editDbPassword = false;
            if (value1[0].equals(key_Username)) {
                val_Username = value1[1];
            } else if (value1[0].equals(key_Alias)) {
                val_Alias = value1[1];
            } else if (value1[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value1[1]);
            } else if (value1[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value1[1]);
            } else if (value1[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value1[1]);
            } else if (value1[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value1[1]);
            } else if (value1[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value1[1]);
            } else if (value1[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value1[1]);
            } else if (value1[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value1[1]);
            }
            if (value2[0].equals(key_Username)) {
                val_Username = value2[1];
            } else if (value2[0].equals(key_Alias)) {
                val_Alias = value2[1];
            } else if (value2[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value2[1]);
            } else if (value2[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value2[1]);
            } else if (value2[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value2[1]);
            } else if (value2[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value2[1]);
            } else if (value2[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value2[1]);
            } else if (value2[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value2[1]);
            } else if (value2[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value2[1]);
            }
            if (value3[0].equals(key_Username)) {
                val_Username = value3[1];
            } else if (value3[0].equals(key_Alias)) {
                val_Alias = value3[1];
            } else if (value3[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value3[1]);
            } else if (value3[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value3[1]);
            } else if (value3[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value3[1]);
            } else if (value3[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value3[1]);
            } else if (value3[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value3[1]);
            } else if (value3[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value3[1]);
            } else if (value3[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value3[1]);
            }
            if (value4[0].equals(key_Username)) {
                val_Username = value4[1];
            } else if (value4[0].equals(key_Alias)) {
                val_Alias = value4[1];
            } else if (value4[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value4[1]);
            } else if (value4[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value4[1]);
            } else if (value4[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value4[1]);
            } else if (value4[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value4[1]);
            } else if (value4[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value4[1]);
            } else if (value4[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value4[1]);
            } else if (value4[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value4[1]);
            }
            if (value5[0].equals(key_Username)) {
                val_Username = value5[1];
            } else if (value5[0].equals(key_Alias)) {
                val_Alias = value5[1];
            } else if (value5[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value5[1]);
            } else if (value5[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value5[1]);
            } else if (value5[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value5[1]);
            } else if (value5[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value5[1]);
            } else if (value5[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value5[1]);
            } else if (value5[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value5[1]);
            } else if (value5[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value5[1]);
            }
            if (value6[0].equals(key_Username)) {
                val_Username = value6[1];
            } else if (value6[0].equals(key_Alias)) {
                val_Alias = value6[1];
            } else if (value6[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value6[1]);
            } else if (value6[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value6[1]);
            } else if (value6[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value6[1]);
            } else if (value6[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value6[1]);
            } else if (value6[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value6[1]);
            } else if (value6[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value6[1]);
            } else if (value6[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value6[1]);
            }
            if (value7[0].equals(key_Username)) {
                val_Username = value7[1];
            } else if (value7[0].equals(key_Alias)) {
                val_Alias = value7[1];
            } else if (value7[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value7[1]);
            } else if (value7[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value7[1]);
            } else if (value7[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value7[1]);
            } else if (value7[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value7[1]);
            } else if (value7[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value7[1]);
            } else if (value7[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value7[1]);
            } else if (value7[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value7[1]);
            }
            if (value8[0].equals(key_Username)) {
                val_Username = value8[1];
            } else if (value8[0].equals(key_Alias)) {
                val_Alias = value8[1];
            } else if (value8[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value8[1]);
            } else if (value8[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value8[1]);
            } else if (value8[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value8[1]);
            } else if (value8[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value8[1]);
            } else if (value8[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value8[1]);
            } else if (value8[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value8[1]);
            } else if (value8[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value8[1]);
            }
            if (value9[0].equals(key_Username)) {
                val_Username = value9[1];
            } else if (value9[0].equals(key_Alias)) {
                val_Alias = value9[1];
            } else if (value9[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value9[1]);
            } else if (value9[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value9[1]);
            } else if (value9[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value9[1]);
            } else if (value9[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value9[1]);
            } else if (value9[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value9[1]);
            } else if (value9[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value9[1]);
            } else if (value9[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value9[1]);
            }


            if (val_Username == null || val_Alias == null) {
                //session.output("modacl - bad cmd\n");
                // output(value1[0]+","+value1[1]+","+value2[0]+","+value2[1]+","+value3[0]+","+value3[1]+","+value4[0]+","+value4[1]+"\n\r"); // diagnostic
            } else {
                if (data.isDBAdmin(session.getUsername(), val_Alias)) {
                    if (data.isDBAdmin(val_Username, val_Alias)) {
                        ACL acl = data.getPermission(val_Username, val_Alias);
                        if (session.getFunct().isAdminACLOk(acl) == false) {
                            boolean ok = data.getSQLManager().updateACL(session.getFunct().correctAdminACL(acl));
                            if (ok == true) {
                                session.netout("modacl-deny-mod-admin");
                            } else {
                                session.netout("modacl-deny-mod-admin");
                            }
                        }
                    } else {
                        ACL acl = new ACL(val_Username, val_Alias, val_showDbPath, val_showDbUsername, val_showDbPassword, val_showDateTimeReg, val_editDbPath, val_editDbUsername, val_editDbPassword);
                        boolean ok = data.changeAccessPermission(session.getUsername(), val_Alias, acl);
                        if (ok == true) {
                            session.netout("modacl-ok");
                        } else {
                            session.netout("modacl-fail");
                        }
                    }
                } else {
                    session.netout("modacl-not-admin");
                }
            }
        }

        if (args[0].equals("isadm")) {
            //session.output("Received 'getdb'\n");
            String[] value1 = args[1].split("=");
            String value = value1[1];
            if (args[1].isEmpty() || (!value1[0].equals("dbalias"))) {
                //session.output("getdb - bad cmd\n");
            } else {
                if (data.isUserExistDB(session.getUsername(), value) == true) {
                    if (data.isDBAdmin(session.getUsername(), value)) {
                        session.netout("isadm-true");
                    } else {
                        session.netout("isadm-false");
                    }
                } else {
                    session.netout("isadm-not-exist");
                }
            }
        }

        if (args[0].equals("getavdbls")) {
            ArrayList dbList = data.getAvDBList(session.getUsername());
            session.netout("getavdblsrs " + procArrayToString(dbList));
        }

        if (args[0].equals("?") || args[0].equals("help")) {
            doHelp();
        }
    }

    public void clearRs() {
        isR = false;
        okRs = false;
        strRs = "";
        intRs = -1;
        aRs.clear();
    }

    /**
     * @return the okRs
     */
    public boolean isOkRs() {
        return okRs;
    }

    /**
     * @param okRs the okRs to set
     */
    public void setOkRs(boolean okRs) {
        this.okRs = okRs;
    }

    /**
     * @return the strRs
     */
    public String getStrRs() {
        return strRs;
    }

    /**
     * @param strRs the strRs to set
     */
    public void setStrRs(String strRs) {
        this.strRs = strRs;
    }

    /**
     * @return the intRs
     */
    public int getIntRs() {
        return intRs;
    }

    /**
     * @param intRs the intRs to set
     */
    public void setIntRs(int intRs) {
        this.intRs = intRs;
    }

    /**
     * @return the aRs
     */
    public ArrayList getARs() {
        return aRs;
    }

    /**
     * @param aRs the aRs to set
     */
    public void setARs(ArrayList aRs) {
        this.aRs = aRs;
    }

    /**
     * @return the isR
     */
    public boolean isIsR() {
        return isR;
    }

    /**
     * @param isR the isR to set
     */
    public void setIsR(boolean isR) {
        this.isR = isR;
    }

    private void doHelp() {
        session.netout("Help" +
                "\n\r====" +
                "\n\r? or help = Help" +
                "\n\rrmme = remove your user account and all profiles link to you" +
                "\n\rmodme oldpwd=<old password> newpwd=<new password> = modify your account" +
                "\n\raddusr usr=<username> pwd=<password>" +
                "\n\rrmusr dbalias=<dbalias> usr=<username>" +
                "\n\rgetdb dbalias=<dbalias>" +
                "\n\rmkdb dbalias=<dbalias> dbusr=<db username> dbpwd=<db password> dbpath=<db path>" +
                "\n\rjoindb dbalias=<dbalias>" +
                "\n\rquitdb dbalias=<dbalias>" +
                "\n\rdeldb dbalias=<dbalias>" +
                "\n\rgetacl usr=<username> dbalias=<dbalias>" +
                "\n\rmodacl usr=<username> dbalias=<dbalias> sdbpath=<boolean> sdbusr=<boolean> sdbpwd=<boolean> sdtreg=<boolean> edbpath=<boolean> edbusr=<boolean> edbpwd=<boolean>\n");
    }

    private String convertBoolToStr(boolean bool) {
        if (bool == true) {
            return "true";
        } else {
            return "false";
        }
    }

    private boolean convertToBool(String string) {
        if (string.equalsIgnoreCase("true")) {
            return true;
        } else {
            return false;
        }
    }

    private String procArrayToString(ArrayList list) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            buf.append("\"" + list.get(i).toString() + "\"");
            buf.append(",");
        }
        return buf.toString();
    }
}
